package com.yipei.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yipei.business.domain.MlTodayProfit;
import com.yipei.business.mapper.MlTodayProfitMapper;
import com.yipei.business.service.IMlTodayProfitService;
import com.yipei.common.utils.DateUtils;
import com.yipei.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 今日利润Service业务层处理
 *
 * @author yipei
 * @date 2024-12-21
 */
@Service
public class MlTodayProfitServiceImpl extends ServiceImpl<MlTodayProfitMapper, MlTodayProfit> implements IMlTodayProfitService {
    @Autowired
    private MlTodayProfitMapper mlTodayProfitMapper;

    /**
     * 查询今日利润
     *
     * @param id 今日利润主键
     * @return 今日利润
     */
    @Override
    public MlTodayProfit selectMlTodayProfitById(Long id) {
        return mlTodayProfitMapper.selectMlTodayProfitById(id);
    }

    /**
     * 查询今日利润列表
     *
     * @param mlTodayProfit 今日利润
     * @return 今日利润
     */
    @Override
    public List<MlTodayProfit> selectMlTodayProfitList(MlTodayProfit mlTodayProfit) {
        return mlTodayProfitMapper.selectMlTodayProfitList(mlTodayProfit);
    }

    /**
     * 新增今日利润
     *
     * @param mlTodayProfit 今日利润
     * @return 结果
     */
    @Override
    public int insertMlTodayProfit(MlTodayProfit mlTodayProfit) {
        mlTodayProfit.setCreateTime(DateUtils.getNowDate());
        return mlTodayProfitMapper.insertMlTodayProfit(mlTodayProfit);
    }

    /**
     * 修改今日利润
     *
     * @param mlTodayProfit 今日利润
     * @return 结果
     */
    @Override
    public int updateMlTodayProfit(MlTodayProfit mlTodayProfit) {
        mlTodayProfit.setUpdateTime(DateUtils.getNowDate());
        return mlTodayProfitMapper.updateMlTodayProfit(mlTodayProfit);
    }

    /**
     * 批量删除今日利润
     *
     * @param ids 需要删除的今日利润主键
     * @return 结果
     */
    @Override
    public int deleteMlTodayProfitByIds(Long[] ids) {
        return mlTodayProfitMapper.deleteMlTodayProfitByIds(ids);
    }

    /**
     * 删除今日利润信息
     *
     * @param id 今日利润主键
     * @return 结果
     */
    @Override
    public int deleteMlTodayProfitById(Long id) {
        return mlTodayProfitMapper.deleteMlTodayProfitById(id);
    }

    @Override
    public void changeProfit(String tdAcc, BigDecimal profit) {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String now = formatter.format(date);
        List<MlTodayProfit> mlTodayProfits = this.lambdaQuery().eq(MlTodayProfit::getTdAcc, tdAcc)
                .eq(MlTodayProfit::getTdDay, now)
                .list();
        if (CollectionUtils.isEmpty(mlTodayProfits)) {
            MlTodayProfit mlTodayProfit = new MlTodayProfit();
            mlTodayProfit.setTdAcc(tdAcc);
            mlTodayProfit.setTdDay(now);
            mlTodayProfit.setProfit(profit);
            mlTodayProfit.setCreateTime(date);
            this.save(mlTodayProfit);
        } else {
            for (MlTodayProfit mlTodayProfit : mlTodayProfits) {
                mlTodayProfit.setTdAcc(tdAcc);
                mlTodayProfit.setTdDay(now);
                mlTodayProfit.setProfit(mlTodayProfit.getProfit().add(profit));
                this.updateById(mlTodayProfit);
            }
        }
    }

    @Override
    public List<MlTodayProfit> tdAccListProfit(MlTodayProfit mlTodayProfit) {
        String tdAcc = SecurityUtils.getLoginMember().getMlTradeAccount().getTdAcc();
        LambdaQueryWrapper<MlTodayProfit> mlTodayProfitLambdaQueryWrapper=new LambdaQueryWrapper<>();
        mlTodayProfitLambdaQueryWrapper.eq(MlTodayProfit::getTdAcc,tdAcc);
        return mlTodayProfitMapper.selectList(mlTodayProfitLambdaQueryWrapper);
    }
}
